import random, pyautogui, asyncio
from db import RolesDB

from core.base_controller import BaseController
from core.utils import focus_wow
from controllers.login_role_controller import LoginRoleController
from controllers.common_actions import CommonActions
from controllers.auto_craft.auto_craft_actions import AutoCraftActions
from controllers.auto_salvage_card.auto_salvage_card_actions import AutoSalvageCardActions

class AutoCraftController(BaseController):
  async def extra_actions(self, log_prefix):
    common_actions = CommonActions(log_prefix=log_prefix, ws_client=self.ws_client)
    auto_craft_actions = AutoCraftActions(log_prefix=log_prefix, ws_client=self.ws_client)
    auto_salvage_card_actions = AutoSalvageCardActions(log_prefix=log_prefix, ws_client=self.ws_client)
    await self.log("extra_actions start", log_prefix=log_prefix)
    
    if self.extra_data.get("reset_keyboard", 0) == 1:
      ## 重置按键
      await common_actions.reset_keymap()
    
    if self.extra_data.get("get_emails", 0) == 1:
      ## 收邮件
      await common_actions.get_emails()
    
    if self.extra_data.get("salvage_card", 0) == 1:
      ## 拆卡牌
      await auto_salvage_card_actions.start_salvage()
      
    profession_type = self.extra_data.get("profession_type", 0)
    craft_dz = profession_type == 0 or profession_type == 1
    craft_fm = profession_type == 0 or profession_type == 2
    if craft_dz:
      ## 切换到锻造桌
      await common_actions.toggle_dz_table()
    if craft_fm:
      ## 打开附魔界面
      await common_actions.open_fm_frame()
      ## 设置当前制造配方
      await auto_craft_actions.set_cur_fm_recipe()
      await asyncio.sleep(random.uniform(1, 2))
    if craft_dz:
      ## 打开锻造界面
      await common_actions.open_dz_frame()
    ## 检查buff
    await common_actions.check_buffs(dh_buff=craft_dz, qs_buff=True)
    ## 制造
    await auto_craft_actions.start_craft(craft_dz=craft_dz, craft_fm=craft_fm)
    ## 发送制造物品
    await auto_craft_actions.send_mail()
    
    await self.log("extra_actions end", log_prefix=log_prefix)